package day_2022_9_to_12.Day;

import java.util.*;

/**
 * @author haomin
 * @date 2022/09/19 08:50
 **/
public class Day17_1_Num1636_FrequencySort {
    class Solution {
        public int[] frequencySort(int[] nums) {
            Map<Integer,Integer> map = new HashMap<>();
            for(int num : nums) map.put(num, map.getOrDefault(num, 0)+1);
            List<int[]> list = new ArrayList<>();
            for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
                list.add(new int[]{entry.getKey(), entry.getValue()});
            }
            Collections.sort(list, (a, b) -> b[1] == a[1] ? b[0] - a[0] : a[1]-b[1] );
            int[] ans = new int[nums.length];
            int i = 0;
            for (int[] ints : list) {
                while (ints[1]-- >0) {
                    ans[i] = ints[0];
                    i++;
                }
            }
            return ans;
        }
    }
}